<?php

namespace App\Http\Middleware;

use Closure;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

//日志需要的model

//日志需要的model

//日志需要的model

class AdminRoustMiddleware
{
    public function __construct()
    {
        $this->log = new Logger('router');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path() . '/logs/router.log', 7, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->logUp = new Logger('routerUp');
        $this->logUp->pushHandler(
            (new RotatingFileHandler(storage_path() . '/logs/routerUp.log', 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

    /**
     * Handle an incoming request.
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // 记录所有的每一步发送请求的日志

        try {
//            $headers = $request->header('Authorization');
//            $params = $request->all();
            $ip = $request->getClientIp();
            $mergeData = ['ip' => $ip];
            if ($request->has('dateRange') && !empty($request->get('dateRange'))) {
                $times = $request->get('dateRange');
                $startTime = trim($times[0]);
                $endTime = trim($times[1]);
                if (strpos($endTime, ' ') === false) {
                    $endTime = $endTime . ' 23:59:59';
                }
//                echo $endTime;
                $mergeData['dateRange'] = [$startTime, $endTime];
//                $request->merge(['dateRange' => [$startTime, $endTime]]);
            }
            $request->merge($mergeData);
            $res = $next($request);
//            // var_dump($res, 2222222222);
//            $ress = json_decode(json_encode($res), true);
//            if ($request->method() == 'get' || $request->method() == 'GET') {
//                // $this->log->info('headers:' . $headers . '参数:' . json_encode($params) . 'ip地址:' . $ip . '返回结果:' . json_encode($ress, JSON_UNESCAPED_UNICODE));
//            } else {
//                // $this->logUp->info('headers:' . $headers . '参数:' . json_encode($params) . 'ip地址:' . $ip . '返回结果:' . json_encode($ress, JSON_UNESCAPED_UNICODE));
//            }

            // var_dump($ress['original'], 111111111);
        } catch (\Exception $e) {
        }

        return $res;
    }
}
